package org.jeecg.modules.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.third.nucarf.NucartUtils;
import org.jeecg.modules.wlhy.gas.service.IHyShippingNoteGasService;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfg;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfgItem;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgItemService;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriverReq;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/api/wlhy/common")
@Api(tags = "常用")
@Slf4j
public class ApiCommonController {

    @Value(value = "${gas.baseUrl}")
    private String baseUrl;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private IHyShippingNoteGasService iHyShippingNoteGasService;

    @Autowired
    private IHyDriverService iHyDriverService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private IHyCfgService hyCfgService;

    @Autowired
    private IHyCfgItemService hyCfgItemService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    //获取万金油页面h5
    @GetMapping(value = "/getWjy")
    public Result<?> getWjy(@RequestParam(name = "id") String id) {

        boolean aBoolean = iHyShippingNoteGasService.checkDriverNameExist(id);


        if (aBoolean) {

            HyConfig gasConfig = HyConfigUtils.getConfig();
            if (gasConfig == null) {
                return Result.error("未找到油气配置信息!");
            }
            if (!gasConfig.getGasIzUse()) {
                return Result.error("油气功能已关闭!");
            }
            String nucarfAppid = gasConfig.getGasAppid();
            String nucarfSecret = gasConfig.getGasSecret();
            if (StringUtils.isEmpty(nucarfAppid) || StringUtils.isEmpty(nucarfSecret)) {
                return Result.error("油气参数错误!");
            }
            String url = baseUrl + "/PublicApi/MemberCart/authMember";
            String sign = NucartUtils.getSign2(nucarfSecret);
            //拼接请求路径
            String http_url = url + "?sign=" + sign + "&appid=" + nucarfAppid;
            return Result.OK(http_url);
        } else {
            return Result.error("您未开通万金油!");
        }


    }

    //获取司机是否开通万金油
    @GetMapping(value = "/getDriverWjy")
    public Result<?> getDriverWjy(@RequestParam(name = "id") String id) {

        boolean aBoolean = iHyShippingNoteGasService.checkDriverNameExist(id);

        if (aBoolean) {
            return Result.OK();
        } else {
            return Result.error("您未开通万金油!");
        }
    }

    //司机重签签名
    @PostMapping(value = "/updateQianMing")
    public Result<?> updateQianMing(@RequestBody JSONObject jo) {

        String driverAutograph = jo.getString("driverAutograph");

        if (StringUtils.isBlank(driverAutograph)) {
            return Result.error("签名图片不允许为空!");
        }
        HyDriver driver = userUtil.getDriver();
        driver.setDriverAutograph(driverAutograph);
        iHyDriverService.updateById(driver);
        return Result.OK();
    }

    //根据货源号获取货源信息
    @GetMapping(value = "/getOrderByNum")
    public Result<?> getOrderByNum(@RequestParam(name = "num") String num) {
        if (StringUtils.isNotEmpty(num)) {
            HyOrder order = hyOrderService.getOne(new QueryWrapper<HyOrder>().eq("order_num", num));
            if (order != null) {
                return Result.OK(order);
            } else {
                return Result.error("未查询到货源信息!");
            }
        } else {
            return Result.error("未查询到货源信息!");
        }

    }

    //
    @GetMapping(value = "/gethyCfgList")
    public Result<?> gethyCfgList() {
        List<HyCfg> list = hyCfgService.list();
        return Result.OK(list);
    }


    @GetMapping(value = "/getLnTaxRealnameAuth")
    public Result<?> getLnTaxRealnameAuth() {
        HyCfgItem hyCfgItem = hyCfgItemService.getByCode("liaoning_tax_report", "realname_auth");
        if (null == hyCfgItem) {
            return Result.OK(false);
        }

        if (!"1".equals(hyCfgItem.getItemValue())) {
            return Result.OK(false);
        }

        return Result.OK(true);
    }


    @GetMapping(value = "/driverQenRen")
    public Result<?> driverQenRen(String id) {
        if(StringUtils.isEmpty(id)){
            return Result.error("参数为空!");
        }
        HyShippingNote shippingNote = hyShippingNoteService.getById(id);
        shippingNote.setModifyFreightStatus("2");
        hyShippingNoteService.updateById(shippingNote);
        return Result.OK();
    }


}
